<template>
      <span>{{time}}</span>
</template>

<script>
   export default{
       data () {
           return {
               time : '',
               flag : false
           }

       },
       mounted () {
        let that=this
          window.setInterval(function(){
            that.timeDown();
          },500)
       },
       props : {
          startTime:{
            type : String
          },
          endTime : {
               type : String
          }
       },
       methods : {
           timeDown () {
              this.startTime=this.startTime.replace(/-/g,'/')
              this.endTime=this.endTime.replace(/-/g,'/')
              const eT = new Date(this.endTime);
              const sT = new Date(this.startTime);
              let nT = new Date();
              let leftTime = parseInt((eT.getTime()-nT.getTime())/1000)
              let rightTime = parseInt((sT.getTime()-nT.getTime())/1000)
          
              if(leftTime<=0){
                this.time = '已结束'
                this.$emit('time-out',true)
               }
               if(rightTime>=0){
                this.time = '未开始'
                this.$emit('time-out',true)
               }
               if(leftTime>0 && rightTime<0){
                 let d = parseInt(leftTime/(24*60*60))
                 let h = this.formate(parseInt(leftTime/(60*60)%24))
                 let m = this.formate(parseInt(leftTime/60%60))
                 let s = this.formate(parseInt(leftTime%60))
              //this.time = `${d}天${h}小时${m}分${s}秒`
                 this.time = `${h}小时${m}分${s}秒`
                 this.$emit('time-out',false)
               }
           },
           formate (time) {
               if(time>=10){
                   return time
               }else{
                   return `0${time}`
               }
           }
       }
   }
</script>